package com.lijin.study.java.thread.test;

public class HelloThread extends Thread {
	
	private static final ThreadLocal<String> THREAD_CONTENT = new ThreadLocal<String>();

	private Counter c;
	
	public HelloThread(Counter c) {
		THREAD_CONTENT.set(this.toString());
		this.c = c;
	}
	
	public void run() {
		THREAD_CONTENT.set(String.valueOf(this.getId()));
		for (int i = 0; i < 10; i ++) {
			c.increment(this);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				System.err.println(e.getMessage());
			}
			c.decrement(this);
		}
		System.out.println("----------->" + THREAD_CONTENT.get());
	}
	
	public static void main(String[] args) {
		Counter c = new Counter();
		new HelloThread(c).start();
		System.out.println(THREAD_CONTENT.get());
		new HelloThread(c).start();
		new HelloThread(c).start();
		System.out.println("Last c is :--->" + c.value());
		new HelloThread(c).start();
		new HelloThread(c).start();
		new HelloThread(c).start();
		new HelloThread(c).start();
		new HelloThread(c).start();
		new HelloThread(c).start();
		new HelloThread(c).start();
		System.out.println("Last c is :--->" + c.value());
		
		System.out.println(THREAD_CONTENT.get());
	}
}
